<?php

abstract class planosDAOImpl {

    public static function Select($search) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id,entitytype, name,price,description,
                autoquantity, daysamount from plans";
            if (isset($search)) {
                if (is_int($search)) {
                    $query = $query . " where id=?";
                } else if (is_string($search)) {
                    $query = $query . " where description=? or name=?";
                }
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_numeric($search)) {
                    DB::$_stmt->bind_param('i', $search);
                } else {
                    DB::$_stmt->bind_param('ss', $search, $searchs);
                }
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result(
                    $data_id, $data_entitytype, $data_name, $data_price, $data_description, $data_autoquantity, $data_daysamount
            );
            while (DB::$_stmt->fetch()) {
                $data[] = array(
                    'id' => $data_id,
                    'entitytype' => $data_entitytype,
                    'name' => $data_name,
                    'price' => $data_price,
                    'description' => $data_description,
                    'autoquantity' => $data_autoquantity,
                    'daysamount' => $data_daysamount
                );
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Listar($search = NULL,$limit = NULL, $page = NULL) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id,entitytype, name,price,description,
                autoquantity, daysamount from plans";
            if (isset($search)) {
                if (is_numeric($search)) {
                    $query = $query . " where id=?";
                } else {
                    $query = $query . " where category = ?";
                }
            }
            if (isset($limit)) {
                $query = $query . " limit " . $limit;
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_numeric($search)) {
                    DB::$_stmt->bind_param('i', $search);
                } else {
                    DB::$_stmt->bind_param('s', $search);
                }
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result(
                    $data_id, $data_entitytype, $data_name, $data_price, $data_description, $data_autoquantity, $data_daysamount
            );
            while (DB::$_stmt->fetch()) {
                $data[] = array(
                    'id' => $data_id,
                    'entitytype' => $data_entitytype,
                    'name' => $data_name,
                    'price' => $data_price,
                    'description' => $data_description,
                    'autoquantity' => $data_autoquantity,
                    'daysamount' => $data_daysamount
                );
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Update($entitytype, $name, $price, $description, $autoquantity, $daysamount,$category,$id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "update plans set entitytype=?,
                name=?,
                price=?,
                description=?,
                autoquantity=?,
                daysamount=?,
                category = ?
                where id=?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ssdsiisi', $entitytype, $name, $price, $description, $autoquantity, $daysamount,$category,$id);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }            
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Insert($entitytype, $name, $price, $description, $autoquantity, $daysamount,$category) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "insert into plans
                (entitytype,name,price,description,autoquantity,daysamount,category) 
                values (?,?,?,?,?,?,?)";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ssdsiis',$entitytype, $name, $price, $description, $autoquantity, $daysamount,$category);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$mysqli->insert_id;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Delete($id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "delete from plans where id = ?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('i', $id);
            DB::$_stmt->execute();
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
        } else {
            error_log("erro ao conectar!");
        }
    }

}
?>
